<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use DB;
use Hash;
use Mail;
use Gregwar\Captcha\CaptchaBuilder;
use App\Http\Requests\AdminloginRequest;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class HomeZhaohuiController extends Controller
{
    public function getIndex()
    {
        return view('home.zhaohui.zhaohui');
    }
	
    //找回密码
    public function postZhaohui(Request $request)
    {
        //判断验证码是否一致
        if($request->input('yanzhengma') != session('Vcode')){
            return back()->with('error','验证码不正确');
        }
        //根据邮箱查询数据
        $res = DB::table('user')->where('email',$request->input('email'))->first();
        //判断
        if($res){
            //发送邮箱
            $this->getEmail($res->id,$request->input('email'),$res->token);
            //解析 发送成功模板
            return view('home.zhaohui.success');
        }else{
            return back()->with('error','请输入正确的邮箱');
        }
    }

    //邮箱
    public function getEmail($id,$email,$token)
    {
         Mail::send('home.zhaohui.yj', ['id'=>$id,'token'=>$token], function ($message)use($email) {
            $message->to($email)->subject('激活邮件');
        });
    }

    //修改密码 视图
    public function getEdit(Request $request)
    {
        $id = $request->input('id');
        $token = $request->input('token');
        //解析视图
        return view('home.zhaohui.edit',['id'=>$id,'token'=>$token]);
    }

    //修改
    public function postUpdate(Request $request)
    {
        
        //验证数据
        $res = DB::table('user')->where('id',$request->input('id'))->first();
        //判断token是否一样
        if($res->token == $request->input('token')){
            //修改数据
            $data['token'] = str_random(50);
            $data['password'] = Hash::make($request->input('password'));
            //修改
            $rs = DB::table('user')->where('id',$request->input('id'))->update($data);
            if($res){
                return redirect('/home/login/login')->with('error','修改成功请登录');
            }else{
                return back()->with('修改失败,请联系管理员');
            }
        }else{
            die('非法操作');
        }
    }

    //判断邮箱是已注册
    public function postInfo(Request $request)
    {   
        $res = DB::table('user')->where('email',$request->input('email'))->first();
        //判断是否有
        if($res){
            echo 1;
        }else{
            echo 2;
        }
    }

    //判断验证吗
    public function postYanzheng(Request $request)
    {
        //验证 验证码是否正确
        if($request->input('ma') != session('Vcode')){
            echo 2;
        }else{
            echo 1;
        }
        
    }
}
